library(maps)
library(countrycode)
library(gapminder)
library(tidyverse)
library(viridis)
library(rio)
library(ggalt)
library(data.table)
library(rworldmap)
load AAAD data
AAAD_merged <- read_csv("AAAD_merged.csv")
# load AAA event data
AAAD_events <- read_csv("_Authoritarian Actions Abroad Database (online appendix).xlsx - Sheet1.csv")
# load coding periods
AAAD_codings <- read_delim("AAAD_countries_years.csv")
# load AAAD data
AAAD_merged <- read_csv("AAAD_merged.csv")
# load AAA event data
AAAD_events <- read_csv("_Authoritarian Actions Abroad Database (online appendix).xlsx - Sheet1.csv")
# load coding periods
AAAD_codings <- read_delim("AAAD_countries_years.csv")
library(countrycode)
library(tidyverse)
library(janitor)
library(wbstats)
library(countrycode)
library(tidyverse)
library(janitor)
library(wbstats)
library(countrycode)
library(tidyverse)
library(janitor)
library(wbstats)
# load AAA event data
AAAD_events <- read_csv("_Authoritarian Actions Abroad Database (online appendix).xlsx - Sheet1.csv")
# load coding periods
AAAD_codings <- read_delim("AAAD_countries_years.csv")
###### Aggregation country-year-target -------------------------------------------
# Aggregate events at the country-year level
AAAD_events_cy_t <- AAAD_events %>%
group_by(country, target, year) %>%
summarise(events = length(ID)) %>%
ungroup() %>%
mutate(year = as.numeric(year)) %>%
drop_na(year)
# pivot to wider
AAAD_events_cy_t_wider <- AAAD_events_cy_t %>%
pivot_wider(values_from= events, names_from = target) %>%
clean_names()
###### Aggregation country-year-action -------------------------------------------
# Aggregate events at the country-year level
AAAD_events_cy_a <- AAAD_events %>%
mutate(action_rec = case_when(action %in% c("family_threatened",
"threatened") ~ "threats",
action %in% c("abduction_attempt",
"assassination_attempt",
"extradition_attempt") ~ "attempts",
action %in% c("abducted", "attacked",
"arrested/detained",
"assassinated",
"extradited") ~ "executed",
T ~ NA_character_)) %>%
drop_na(action_rec) %>%
group_by(country, action_rec, year) %>%
summarise(events = length(ID)) %>%
ungroup() %>%
mutate(year = as.numeric(year)) %>%
drop_na(year)
# pivot to wider
AAAD_events_cy_a_wider <- AAAD_events_cy_a %>%
pivot_wider(values_from= events, names_from = action_rec) %>%
clean_names()
# Merge event country years with coding periods, replace NAs with 0
# Sum number of all events
AAAD_codings_events <- left_join(AAAD_codings, AAAD_events_cy_t_wider) %>%
left_join(., AAAD_events_cy_a_wider) %>%
replace(is.na(.), 0) %>%
mutate(all_events = rowSums(select(., activist, citizen, former_government_official,
journalist, opposition)),
iso3c = countrycode(country, "country.name", "iso3c"), .after = country) %>%
arrange(iso3c, year) %>%
group_by(iso3c, country) %>%
mutate(first_year = min(year, na.rm = T) - 1) %>%
group_modify(~ add_row(.x, .before=0)) %>%
mutate(year = ifelse(is.na(year), min(first_year, na.rm = T), year)) %>%
arrange(iso3c, year) %>%
ungroup() %>%
select(-first_year)
##### Add data from WB ####
# Population, total
pop_data <- wb_data("SP.POP.TOTL",
gapfill = T, mrv = 35) %>%
select(iso3c, year = date, pop = SP.POP.TOTL)
# GDP per capita
gdp_data <- wb_data("NY.GDP.PCAP.CD",  gapfill = T, mrv = 35) %>%
select(iso3c, year = date, gdp_pc = NY.GDP.PCAP.CD)
# GDP growth
gdp_growth_data <- wb_data("NY.GDP.MKTP.KD.ZG",  gapfill = T, mrv = 35) %>%
select(iso3c, year = date, gdp_growth = NY.GDP.MKTP.KD.ZG)
# natural resources
natural_resources <- wb_data("NY.GDP.TOTL.RT.ZS", gapfill = T, mrv = 35) %>%
select(iso3c, year = date, nat_resources = NY.GDP.TOTL.RT.ZS)
# military personell
#military <- wb_data("MS.MIL.XPND.ZS", gapfill = T, mrv = 35) %>%
#  select(iso3c, year = date, military = MS.MIL.XPND.ZS)
##### Add military data data #####
military_wb <- rio::import("data/API_MS.MIL.XPND.CD_DS2_en_excel_v2_4256264.xls") %>%
clean_names() %>%
select(iso3c = country_code, x1960:x2021) %>%
pivot_longer(cols = starts_with("x"),
names_to = "year",
values_to = "military",
names_prefix = "x") %>%
mutate(year = as.numeric(year))
# describe data
Hmisc::describe(natural_resources$nat_resources)
##### Add V-Dem data #####
vdem <- vdemdata::vdem %>%
mutate(iso3c = countrycode(country_name, "country.name", "iso3c"))  %>%
select(iso3c, year, v2x_libdem, v2x_polyarchy, v2csreprss, v2x_civlib,
e_polity2, v2eltype_0, v2eltype_6, v2x_ex_hereditary, v2cademmob,
v2x_ex_military, v2x_ex_party, v2x_clphy, v2x_clpol, v2x_clpriv,
v2clkill, v2cltort, v2mecenefm, v2meharjrn, v2meslfcen, v2cldiscm,
v2cldiscw, v2psparban, v2psbars, v2psoppaut, v2cseeorgs,v2csreprss,
v2clslavem, v2clslavef, v2clprptym, v2clprptyw, v2clfmove,v2cldmovem,
v2cldmovew, v2clrelig,v2csrlgrep) %>%
filter(year > 1989) %>%
replace_na(list(v2eltype_0 = 0, v2eltype_6 = 0))
#### Add data from Fariss #####
human_rights <- read_csv("data/HumanRightsProtectionScores_v4.01.csv") %>%
filter(YEAR > 1989) %>%
mutate(iso3c = countrycode(country_name, "country.name", "iso3c"))  %>%
select(iso3c, year = YEAR, theta_mean, Amnesty, killing_best)
# describe data
Hmisc::describe(data_final$nat_resources)
##### Add UCDP data on one sided violence ####
ucdp <- read_csv("data/ucdp-onesided-221.csv") %>%
filter(is_government_actor == 1) %>%
mutate(iso3c = countrycode(gwnoa, "gwn", "iso3c")) %>%
select(iso3c, year, best_fatality_estimate)
##### Add REIGN data #####
reign <- read_csv("data/REIGN_2021_4.csv") %>%
mutate(iso3c = countrycode(ccode, "cown", "iso3c"))
reign_cy <- reign %>%
group_by(iso3c, year) %>%
summarise(political_violence = mean(political_violence, na.rm=T),
coup_risk = mean(couprisk, na.rm = T),
tenure = max(tenure_months, na.rm = T))
#### Merge all data #####
data_final <- left_join(AAAD_codings_events, pop_data, by = c("year", "iso3c")) %>%
left_join(., gdp_data, by = c("year", "iso3c")) %>%
left_join(., gdp_growth_data, by = c("year", "iso3c")) %>%
left_join(., natural_resources, by = c("year", "iso3c")) %>%
#  left_join(., military_wb, by = c("year", "iso3c")) %>%
left_join(., vdem, by = c("year", "iso3c")) %>%
left_join(., human_rights, by = c("year", "iso3c")) %>%
left_join(., ucdp, by = c("year", "iso3c")) %>%
left_join(., reign_cy, by = c("year", "iso3c")) %>%
distinct(iso3c, year, .keep_all = TRUE) %>%
arrange(iso3c, year)  %>%
mutate(one_sided = ifelse(is.na(best_fatality_estimate), 0, 1))
save(data_final, file = "data/data_final.Rda")
load("/Users/redmondscales/Dropbox/Transnational repression/TR_project_share/TR_project_share/data/data_final.Rda")
AAAD_merged <- read_csv("AAAD_merged.csv")
# load AAA event data
AAAD_events <- read_csv("_Authoritarian Actions Abroad Database (online appendix).xlsx - Sheet1.csv")
# load coding periods
AAAD_codings <- read_delim("AAAD_countries_years.csv")
